<?php

$max_shift = MAX_SHIFT_LEN;

$results = lomgetit($sql);
print "<h2>Need</h2>";
simple_table($results);
$i=0;

$start = $results[0]['start'];
$stop = $results[0]['stop'];
$need = $results[0]['auto'];

$i=0;
while ($start !== $stop){
	$gas = get_avail_start($start);
	if (count($gas) == 0){print "Need more Avail"; die();}
	$min_time = $gas[0]['min'] * 3600 + $start;
	$max_time = $gas[0]['max'] * 3600 + $start;
	$auto = $gas[0]['auto'];
	$employee = $gas[0]['employee'];


	if ($min_time > $stop){print "FAIL\n"; die();}

	//print "<h2>Avail at $start</h2>";
	//simple_table($gas);

	if ($stop > $max_time){$shift_end = $max_time;}
	else {$shift_end = $stop;}

	$len = ($shift_end - $start)/3600;

	$verified = verify_scheduling_protocols($start, $shift_end, $employee);
	if ($verified == 0){print "Didn't Pass\n"; die();;}


	$OUT[$i]['avail'] = $auto;
	$OUT[$i]['employee'] = $employee;
	$OUT[$i]['start'] = $start;
	$OUT[$i]['stop'] = $shift_end;

	$start = $shift_end;
	$i++;
}

print_r($OUT);

foreach ($OUT as $o){

	//insert into sked table
	$sql  = "insert into sked ";
	$sql .= "set start = {$o['start']},";
	$sql .= "stop = {$o['stop']},";
	$sql .= "employee = {$o['employee']}";
	lominsert($sql);


	//mark as processed in avail
	$sql  = "update avail ";
	$sql .= "set processed = 1 ";
	$sql .= "where auto = {$o['avail']}";
	lominsert($sql);

}

//mark as processed in need
$sql  = "update need ";
$sql .= "set processed = 1 ";
$sql .= "where auto = {$need}";
lominsert($sql);

